package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Board;
import com.example.forum.service.IBoardService;
import jakarta.annotation.Resource;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述:
 *
 * @author Lenovo
 * @date 2024/12/13
 */

@Slf4j
@RestController
@RequestMapping("/board")
public class BoardController {
    //配置文件中获取显示个数
    @Value("${forum.index.board-num:9}")
    private Integer indexBoardNum;

    @Resource
    private IBoardService boardService;

    /**
     * 查询板块列表
     *
     * @return
     */
    @GetMapping("/topList")
    public AppResult<List<Board>> topList() {
        log.info("板块个数：" + indexBoardNum);
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        //为空的话返回空集合，不返回null
        if (boards == null) {
            boards = new ArrayList<>();
        }
        return AppResult.success(boards);
    }

    @GetMapping("/getById")
    public AppResult<Board> getById(@RequestParam("id") @NonNull Long id) {
        Board board = boardService.selectById(id);
        //校验板块信息
        if (board == null || board.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //返回结果
        return AppResult.success(board);
    }
}
